home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Tools / Languages / Icon 8.1 / msm-1 / common.sit / time.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-09-19  |  7.5 KB  |  377 lines  |  [TEXT/MPS ]

  1. #include "::h:gsupport.h"
  2.  
  3. /*
  4.  * The following code is operating-system dependent [@time.01].  Include files
  5.  *  that are system-dependent.
  6.  */
  7.  
  8. #if PORT
  9.    /* probably needs something */
  10. Deliberate Syntax Error
  11. #endif                    /* PORT */
  12.  
  13. #if AMIGA
  14. #include "time.h"
  15. #endif                    /* AMIGA */
  16.  
  17. #if ATARI_ST
  18.    /* nothing is needed */
  19. #endif                    /* ATARI_ST */
  20.  
  21. #if ARM || MVS || VM
  22. #include <time.h>
  23. #endif                    /* ARM || MVS || ... */
  24.  
  25. #if MACINTOSH
  26. #if LSC
  27. #include <time.h>
  28. #endif                    /* LSC */
  29. #if MPW
  30. #include <types.h>
  31. #include "time.h"
  32. #include <OSUtils.h>
  33. #include <Events.h>
  34. #endif                    /* MPW */
  35. #endif                    /* MACINTOSH */
  36.  
  37. #if MSDOS
  38. #include <time.h>
  39. #if MICROSOFT
  40. #include <sys/types.h>
  41. #endif                    /* MICROSOFT */
  42. #endif                    /* MSDOS */
  43.  
  44. #if OS2
  45. #include <time.h>
  46. #include <sys/types.h>
  47. #endif                    /* OS2 */
  48.  
  49. #if UNIX
  50. #ifdef CRAY
  51. #define word    fubar_word
  52. #include <sys/types.h>
  53. #include <sys/times.h>
  54. #undef word
  55. #else                    /* CRAY */
  56. #include <sys/types.h>
  57. #include <sys/times.h>
  58. #endif                    /* CRAY */
  59. #include SysTime
  60. #endif                    /* UNIX */
  61.  
  62. #if VMS
  63. #include <types.h>
  64. #include <time.h>
  65. struct tms {
  66.     time_t    tms_utime;    /* user time */
  67.     time_t    tms_stime;    /* system time */
  68.     time_t    tms_cutime;    /* user time, children */
  69.     time_t    tms_cstime;    /* system time, children */
  70. };
  71. #endif                    /* VMS */
  72.  
  73. /*
  74.  * End of operating-system specific code.
  75.  */
  76.  
  77. int first_time = 1;
  78.  
  79. static char *day[] = {
  80.    "Sunday", "Monday", "Tuesday", "Wednesday",
  81.    "Thursday", "Friday", "Saturday"
  82.    };
  83.  
  84. static char *month[] = {
  85.    "January", "February", "March", "April", "May", "June",
  86.    "July", "August", "September", "October", "November", "December"
  87.    };
  88.  
  89.  
  90. /*
  91.  * getitime - fill in a "struct cal_time" with information about the current
  92.  *  time and date.
  93.  */
  94. novalue getitime(ct)
  95. struct cal_time *ct;
  96.    {
  97.  
  98. /*
  99.  * The following code is operating-system dependent [@time.02]. Declarations
  100.  *  for getting time.
  101.  */
  102.  
  103. #if PORT
  104.    long time();
  105.    long xclock;
  106. Deliberate Syntax Error
  107. #endif                    /* PORT */
  108.  
  109. #if AMIGA || OS2 || UNIX
  110.    long time();
  111.    long xclock;
  112. #endif                    /* AMIGA || OS2 || UNIX */
  113.  
  114. #if ARM || VMS
  115.    time_t xclock;
  116. #endif                    /* ARM || VMS */
  117.  
  118. #if ATARI_ST
  119.    struct tm {
  120.        short tm_year;
  121.        short tm_mon;
  122.        short tm_wday;
  123.        short tm_mday;
  124.        short tm_hour;
  125.        short tm_min;
  126.        short tm_sec;
  127.    };
  128.    long xclock;
  129. #endif                    /* ATARI_ST */
  130.  
  131. #if MACINTOSH
  132. #if LSC
  133.    unsigned long xclock;
  134.    unsigned long time();
  135. #else                    /* LSC */
  136.    time_t xclock;
  137. #endif                    /* LSC */
  138. #endif                    /* MACINTOSH */
  139.  
  140. #if MSDOS
  141. #if MICROSOFT
  142.    time_t time();
  143.    long xclock;
  144. #endif                    /* MICROSOFT */
  145. #if LATTICE || TURBO || INTEL_386 || ZTC_386
  146.    long time();
  147.    long xclock;
  148. #endif                    /* LATTICE || MICROSOFT || ... */
  149. #if HIGHC_386
  150.    long time();
  151.    time_t xclock;
  152. #endif                    /* HIGHC_386 */
  153. #endif                    /* MSDOS */
  154.  
  155. #if MVS || VM
  156.    time_t xclock;
  157. #endif                    /* MVS || VM */
  158.  
  159. /*
  160.  * End of operating-system specific code.
  161.  */
  162.  
  163.    struct tm *tbuf, *localtime();
  164. /*
  165.  * The following code is operating-system dependent [@time.03]. Code for
  166.  *  getting time.
  167.  */
  168.  
  169. #if PORT
  170.    time(&xclock);
  171.    tbuf = localtime(&xclock);
  172. Deliberate Syntax Error
  173. #endif                    /* PORT */
  174.  
  175. #if AMIGA || ARM || MACINTOSH || MSDOS || OS2 || UNIX || VMS || MVS || VM
  176.    time(&xclock);
  177.    tbuf = localtime(&xclock);
  178. #endif                    /* AMIGA || ARM || ... */
  179.  
  180. #if ATARI_ST
  181.     tbuf = localtime(&xclock);
  182. #endif                    /* ATARI_ST */
  183.  
  184. /*
  185.  * End of operating-system specific code.
  186.  */
  187.  
  188.    ct->year = 1900 + tbuf->tm_year;
  189.    ct->month_no = tbuf->tm_mon+1;
  190.    ct->month_nm = month[tbuf->tm_mon];
  191.    ct->mday = tbuf->tm_mday;
  192.    ct->wday = day[tbuf->tm_wday];
  193.    ct->hour = tbuf->tm_hour;
  194.    ct->minute = tbuf->tm_min;
  195.    ct->second = tbuf->tm_sec;
  196.    return;
  197.    }
  198.  
  199. /*
  200.  * getctime - fill a buffer with the "ctime" representation of the current
  201.  *  time and date. The buffer must be at least 26 characters.
  202.  */
  203. novalue getctime(sbuf)
  204. char *sbuf;
  205.    {
  206.    struct cal_time ct;
  207.  
  208.    getitime(&ct);
  209.    sprintf(sbuf, "%.3s %.3s%3d %.2d:%.2d:%.2d %d\n", ct.wday, ct.month_nm,
  210.       ct.mday, ct.hour, ct.minute, ct.second, ct.year);
  211.    return;
  212.    }
  213.  
  214. /*
  215.  * millisec - returns execution time in milliseconds. Time is measured
  216.  *  from the function's first call. The granularity of the time is
  217.  *  generally more than one millisecond and on some systems it my only
  218.  *  be accurate to the second.
  219.  */
  220. long millisec()
  221.    {
  222.  
  223. /*
  224.  * The following code is operating-system dependent [@time.04]. Declarations
  225.  *   that are system-dependent.
  226.  */
  227.  
  228. #if PORT
  229.    static long starttime;
  230.    long time();
  231. Deliberate Syntax Error
  232. #endif                    /* PORT */
  233.  
  234. #if AMIGA || ATARI_ST
  235.    static long starttime;
  236.    long time();
  237. #endif                    /* AMIGA || ATARI_ST */
  238.  
  239. #if ARM
  240.    static clock_t strtime;
  241. #endif                    /* ARM */
  242.  
  243.  
  244. #if MACINTOSH
  245.    static long starttime;
  246. #endif                    /* MACINTOSH */
  247.  
  248. #if MSDOS
  249. #if MICROSOFT
  250.    static long starttime;
  251.    time_t time();
  252. #endif                    /* MICROSOFT */
  253. #if LATTICE || TURBO || INTEL_386 || ZTC_386
  254.    static long starttime;
  255.    long time();
  256. #endif                    /* LATTICE || MICROSOFT || TURBO */
  257. #if HIGHC_386
  258.    static clock_t hc_strtime;
  259. #endif                    /* HIGHC_386 */
  260. #endif                    /* MSDOS */
  261.  
  262. #if MVS || VM || OS2
  263.    static clock_t starttime;
  264. #endif                                  /* MVS || VM */
  265.  
  266. #if UNIX || VMS
  267.    struct tms tp;
  268.    static long starttime;
  269. #endif                    /* UNIX || VMS */
  270.  
  271. /*
  272.  * End of operating-system specific code.
  273.  */
  274.  
  275.    if (first_time) {
  276.       first_time = 0;
  277.  
  278. /*
  279.  * The following code is operating-system dependent [@time.05].  Get start
  280.  *  time.
  281.  */
  282.  
  283. #if PORT
  284.       /* needs something */
  285. Deliberate Syntax Error
  286. #endif                    /* PORT */
  287.  
  288. #if AMIGA || ATARI_ST
  289.       time(&starttime);    /* note: this obtains time in various units */
  290. #endif                    /* AMIGA || ATARI_ST */
  291.  
  292. #if MSDOS
  293. #if MICROSOFT || TURBO || ZTC_386
  294.       time(&starttime);    /* note: this obtains time in various units */
  295. #endif                    /* MICROSOFT || TURBO */
  296. #if INTEL_386
  297.       starttime = (long)clock();
  298. #endif                    /* INTEL_386 */
  299. #if HIGHC_386
  300.       hc_strtime = clock();
  301. #endif                    /* HIGHC_386 */
  302. #endif                    /* MSDOS */
  303.  
  304. #if ARM
  305.       strtime = clock();
  306. #endif                    /* ARM */
  307.  
  308.  
  309. #if MACINTOSH
  310.       starttime = TickCount();    /* 60 ticks / second */
  311. #endif                    /* MACINTOSH */
  312.  
  313. #if MVS || VM || OS2
  314.       starttime = clock();        /* clock ticks */
  315. #endif                    /* MVS || VM || OS2 */
  316.  
  317. #if UNIX || VMS
  318.       times(&tp);
  319.       starttime = tp.tms_utime;
  320. #endif                    /* UNIX || VMS */
  321.  
  322. /*
  323.  * End of operating-system specific code.
  324.  */
  325.  
  326.       return 0L;
  327.       }
  328.    else {    /* not first time */
  329. /*
  330.  * The following code is operating-system dependent [@time.06].  Get time.
  331.  */
  332.  
  333. #if PORT
  334.    /* needs something */
  335. Deliberate Syntax Error
  336. #endif                    /* PORT */
  337.  
  338. #if MSDOS || AMIGA
  339. #if HIGHC_386
  340.       return (clock() - hc_strtime) * 10L;    /* thousandths */
  341. #endif                    /* HIGHC_386 */
  342. #if INTEL_386
  343.       return ((long)clock() - starttime) * (1000L / CLOCKS_PER_SEC);
  344. #endif                    /* INTEL_386 */
  345. #if MICROSOFT || TURBO || LATTICE || MWC
  346.       return 1000 * (time(NULL) - starttime);
  347. #endif                    /* HIGHC_386 */
  348. #endif                    /* AMIGA || MSDOS */
  349.  
  350. #if ARM
  351.       /* thousandths; assumes CLOCKS_PER-SEC is 100 */
  352.       return (clock() - strtime) * 10L;
  353. #endif                    /* ARM */
  354.  
  355. #if ATARI_ST
  356.       return (time(NULL) - starttime) / 10;
  357. #endif                    /* ATARI_ST */
  358.  
  359. #if MACINTOSH
  360.       return 1000 * ((extended)(TickCount() - starttime) / (extended)Hz);
  361. #endif                    /* MACINTOSH */
  362.  
  363. #if MVS || VM || OS2
  364.       return (1000.0/CLOCKS_PER_SEC) * (clock() - starttime);
  365. #endif                    /* MVS || VM */
  366.  
  367. #if UNIX || VMS
  368.       times(&tp);
  369.       return 1000 * ((tp.tms_utime - starttime) / (double)Hz);
  370. #endif                    /* UNIX || VMS */
  371.  
  372. /*
  373.  * End of operating-system specific code.
  374.  */
  375.       }
  376.    }
  377.